package edu.software.PBL_Management_System.comment.controller;

import edu.software.PBL_Management_System.comment.service.CommentService;
import edu.software.PBL_Management_System.entity.Comment;
import edu.software.PBL_Management_System.entity.Demand;
import jakarta.annotation.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

/*
POST/api/comments- 创建新评论
POST/api/comments/{commentId}/reply- 回复评论
GET/api/comments/demand/{demandId}- 获取需求的评论列表
GET/api/comments/{commentId}/replies- 获取评论的回复列表
POST/api/comments/user/received- 获取用户收到的评论
PUT/api/comments/{commentId}/read- 标记评论为已读
GET/api/comments/user/{userId}/unread/count- 获取用户未读评论数量
GET/api/comments/demand/{demandId}/count-获取需求评论数量
GET/api/comments/demand/{demandId}/unread/count-获取需求未读评论数量
GET/api/comments/demand/latest-time-获取需求最新消息时间
GET/api/comments/expert/{userId}/demands-获取专家评论过的需求列表
DELETE/api/comments/{commentId}- 删除评论
*/
@RestController
@RequestMapping("/api/comments")
public class CommentController {

    //依赖注入
    @Resource
    private CommentService commentService;

    // 创建评论
    @PostMapping
    public ResponseEntity<Comment> createComment(@RequestBody Comment comment) {
        return ResponseEntity.ok(commentService.createComment(comment));
    }

    // 回复评论
    @PostMapping("/{commentId}/reply")
    public ResponseEntity<Comment> replyToComment(
            @PathVariable Long commentId,
            @RequestBody Comment reply) {
        return ResponseEntity.ok(commentService.replyToComment(commentId, reply));
    }

    // 获取需求的评论列表
    @GetMapping("/demand/{demandId}")
    public ResponseEntity<List<Comment>> getDemandComments(@PathVariable Long demandId) {
        return ResponseEntity.ok(commentService.getDemandComments(demandId));
    }

    // 获取评论的回复列表
    @GetMapping("/{commentId}/replies")
    public ResponseEntity<List<Comment>> getCommentReplies(@PathVariable Long commentId) {
        return ResponseEntity.ok(commentService.getCommentReplies(commentId));
    }

    // 获取用户收到的评论
    @PostMapping("/user/received")
    public ResponseEntity<List<Comment>> getUserReceivedComments(@RequestBody List<Long> demandIds) {
        return ResponseEntity.ok(commentService.getUserReceivedComments(demandIds));
    }

    // 标记评论为已读
    @PutMapping("/{commentId}/read")
    public ResponseEntity<Void> markCommentAsRead(@PathVariable Long commentId) {
        commentService.markCommentAsRead(commentId);
        return ResponseEntity.ok().build();
    }

    // 获取用户未读评论数量
    @GetMapping("/user/{userId}/unread/count")
    public ResponseEntity<Long> getUnreadCommentCount(@PathVariable Long userId) {
        return ResponseEntity.ok(commentService.getUnreadCommentCount(userId));
    }

    // 获取需求评论数量
    @GetMapping("/demand/{demandId}/count")
    public ResponseEntity<Long> getDemandCommentCount(@PathVariable Long demandId) {
        return ResponseEntity.ok(commentService.getDemandCommentCount(demandId));
    }

    // 获取需求未读评论数量
    @GetMapping("/demand/{demandId}/unread/count")
    public ResponseEntity<Long> getDemandUnreadCount(@PathVariable Long demandId) {
        return ResponseEntity.ok(commentService.getDemandUnreadCount(demandId));
    }

    // 获取需求最新消息时间
    @GetMapping("/demand/latest-time")
    public ResponseEntity<Map<Long, String>> getDemandsLatestMessageTime(@RequestParam List<Long> demandIds) {
        return ResponseEntity.ok(commentService.getDemandsLatestMessageTime(demandIds));
    }

    // 获取专家评论过的需求列表
    @GetMapping("/expert/{userId}/demands")
    public ResponseEntity<List<Demand>> getExpertCommentedDemands(@PathVariable Long userId) {
        return ResponseEntity.ok(commentService.getExpertCommentedDemands(userId));
    }

    // 删除评论
    @DeleteMapping("/{commentId}")
    public ResponseEntity<Void> deleteComment(@PathVariable Long commentId) {
        commentService.deleteComment(commentId);
        return ResponseEntity.ok().build();
    }
}
